package com.example.demo.controller;

import com.example.demo.common.utills.CommonResult;
import com.example.demo.common.utills.FileUtil;
import com.example.demo.mapper.SharedPlatformMapper;
import com.example.demo.service.DataReportsService;
import com.example.demo.service.SharedPlatformService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;

@RestController
@CrossOrigin(allowCredentials = "true")
public class SharedPlatformController {

    @Resource
    SharedPlatformService sharedPlatformService;

    @Resource
    SharedPlatformMapper sharedPlatformMapper;

    @Resource
    HttpServletResponse httpServletResponse;

    @RequestMapping("/share_table")
    public CommonResult data_table(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
                                   @RequestParam(value = "name", required = false, defaultValue = "") String name) {
        return sharedPlatformService.selectAll(pageNum, pageSize, name);
    }

    @RequestMapping("/SFileLoadDown")
    public CommonResult fileDown(int id) throws FileNotFoundException, UnsupportedEncodingException {
        String path = sharedPlatformMapper.queryPath(id);
        FileUtil.downloadLocal(httpServletResponse,path);
        return new CommonResult(200,"下载成功");
    }

    @RequestMapping("/deleteSh")
    public CommonResult delete(int share_id){
        String path = sharedPlatformMapper.queryPath(share_id);
        String s = FileUtil.deleteFile(path);
        if(s.equals("success")){
            sharedPlatformMapper.delete(share_id);
            return new CommonResult(200,"删除成功");
        }
        return new CommonResult(500,"删除失败");
    }
}
